<template>
	<div class="u-bg-fff">
		<div class="u-bg-fff u-p-20">
			<el-form :model="model" ref="uForm" :rules="rules" :errorType="errorType">
				<el-form-item label="姓名" prop="userName" class="u-m-b-20">
					<el-input v-model="model.userName" placeholder="请输入姓名" />
				</el-form-item>
				<el-form-item label="密码" prop="password" class="u-m-b-20">
					<el-input :password-icon="true" type="password" v-model="model.password" placeholder="请输入密码">
					</el-input>
				</el-form-item>
				<el-form-item label="确认密码" label-width="150" prop="rePassword" class="u-m-b-20">
					<el-input type="password" v-model="model.rePassword" placeholder="请确认密码" />
				</el-form-item>

				<div style="text-align: center;">
					<el-button @click="submit('uForm')" type="primary">提交</el-button>
				</div>
			</el-form>

		</div>
	</div>
</template>

<script>
	import FWRSAHelper from "../../utils/RSA.js"
	import {
		Session
	} from '@/utils/storage';
	let that;
	export default {
		props: ["phoneNum"],
		data() {
			return {
				model: {
					userName: '',
					phoneNum: '',
					password: '',
					rePassword: ''
				},

				rules: {
					userName: [{
						required: true,
						message: '请填写姓名',
						trigger: ['change', 'blur']
					}],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							// 正则不能含有两边的引号
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{5,12}$/,
							message: '需同时含有字母和数字，长度在6-12之间',
							trigger: ['change', 'blur'],
						}
					],
					rePassword: [{
							required: true,
							message: '请重新输入密码',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.password;
							},
							message: '两次输入的密码不相等',
							trigger: ['change', 'blur'],
						}
					],
				},
				errorType: ['message'],

			}
		},

		mounted() {
			that = this
			if (that.phoneNum) that.model.phoneNum = that.phoneNum
		},

		methods: {

			submit(formName) {
				//console.log("sss", formName)
				// this.$refs[formName].validate(valid => {
				// 	console.log('valid')

				// 	if (valid) {

				// 		} else {
				// 			that.common.showToast("请填写完整")
				// 		}
				// 	} else {
				// 		console.log('验证失败');
				// 	}
				// });

				if (that.model.userName && that.model.phoneNum && that.model.password) {

					that.ajax({
						url: 'userRegister/userRegisterSave',
						data: {
							userName: that.model.userName,
							phoneNum: that.model.phoneNum,
							password: FWRSAHelper.FWRSAHelper.encrypt(that.model.password),
						},
						contentType: 'application/x-www-form-urlencoded',
						success: function(result) {
							if (result.data && result.data.ticketId) {
								that.loginIn()
							}
						}
					});
				} else {
					return that.$message.error('请填写完整');
				}
			},


			loginIn() {
				that.ajax({
					url: '/userLogin/login',
					data: {
						phoneNum: that.model.phoneNum,
						password: FWRSAHelper.FWRSAHelper.encrypt(that.model.password)
					},
					contentType: 'application/x-www-form-urlencoded',
					success: (res) => {
						var result = res;

						if (result.status == 1 && result.data.ticketId) {							
							let thisUser = result.data.user
							thisUser.nickName = thisUser.userName
							thisUser.userName = 'admin'
							thisUser.roles = ['admin'];
							thisUser.authBtnList = ['btn.add', 'btn.del', 'btn.edit', 'btn.link'];
							let userInfo = JSON.stringify(result.data.user)
							
							if (result.data && result.data.user) localStorage.setItem("user", userInfo)
							
							// 存储 token 到浏览器缓存
							Session.set('token', result.data.ticketId)
							// 存储用户信息到浏览器缓存
							Session.set('userInfo', thisUser);
							// 存储用户信息到vuex
							that.$store.dispatch('userInfos/setUserInfos', thisUser);
							that.$store.commit('setToken', result.data.ticketId);
							//PrevLoading.start();
							that.$message.success("注册成功，正在跳转")
							setTimeout(function(){
								that.$router.push('/home');
							},1000)
							
						} else {
							return that.$message.error('账号或密码错误');
						}
					},
					fail: (res) => {
						return that.$message.error('登录验证失败');
					},
					complete: function(res) {
						console.log(res);
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
